<?php
function post($url,$param){
    if(!is_array($param)){
        throw new Exception("参数必须为array");
    }
    try {
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_POST,1);
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$param);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_TIMEOUT,RedirectTime);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array(
            'Content_Type:application/json;charset=utf-8',
            'Content_Length:'.strlen($param)));
        $ret = curl_exec($ch);
        $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        curl_close($ch);
        return array($code,$ret);
    } catch(\Exception $e) {
        $this->error('error');
    }
}

function get($url, $param=array()){
    if(!is_array($param)){
        throw new Exception("参数必须为array");
    }
    $p='';
    foreach($param as $key => $value){
        $p=$p.$key.'='.$value.'&';
    }
    if(preg_match('/\?[\d\D]+/',$url)){//matched ?c
        $p='&'.$p;
    }else if(preg_match('/\?$/',$url)){//matched ?$
        $p=$p;
    }else{
        $p='?'.$p;
    }
    $p=preg_replace('/&$/','',$p);
    $url=$url.$p;
//    dump($url);
    try {
        $ch =curl_init($url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch,CURLOPT_HTTPHEADER,array(
            'Content_Type:application/json;charset=utf-8',
            'Content_Length:'.strlen($param)));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $ret=curl_exec($ch);
        $code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
        curl_close($ch);
        return array($code,$ret);
    } catch(\Exception $e) {
        $this->error('error');
    }
}
